FUNCTION ... END FUNCTION
FUNCTION name ( [arg [= expr]{,arg [= expr]} ] [, ...] )
[ DIM variable {, variable } ]
[ STATIC variable {, variable } ]
[ SHARED variable {, variable } ]
[ RETURN expr {, expr} ]
[ EXIT FUNCTION ]
{ statement }
END FUNCTION
Define a function. Unlike many BASICs, more than one value may be returned from a function. As a result, the Return keyword is used to return values from functions. If no value is explicitly returned, the value Nothing is returned.
FUNCTION addOne( n )
RETURN n + 1
END FUNCTION
You can have optional values in parameters. If these parameters are are not included in the function call, they are assigned the default value:
FUNCTION hasOptional( a, b=10, c="default string" )
PRINT "a=", a
PRINT "b=", b
PRINT "c=", c
END FUNCTION
You can use Static variables in functions. These are variables that retain their value. The initial value of Static variables is Nothing:
FUNCTION accum( n )
' declare result as a STATIC variable
STATIC result
' first time calling accum?
IF result = NOTHING THEN
' initialize the result
result = n
ELSE
' add value to the result
result = result + n
END IF
' return accumulated result
RETURN result
END FUNCTION
Some or all of the return values may be discarded by the caller. If a function returns more values than requested, the extra values are discarded. If the function returns less values than expected, the extra variables are assigned the value Nothing:
FUNCTION returnThreeValues()
RETURN 1, 2, 3
END FUNCTION
' ignore all values
returnThreeValues()
' ignore the last value
a, b = returnThreeValues
' Nothing is assigned to d
a, b, c, d = returnThreeValues()
index function;